import { message } from "antd";

//第一次封装，提取公共接口等
const baseUrl = "http://jacklv.cn"

function request(url, options = {}) {
    if (options.method && options.method.toLowerCase() !== 'get') {
        //请求方式如果不是get，则需要处理body
        options.body = JSON.stringify(options.body || {});
        options.headers = {
            "Content-Type": "application/json",
        }
    }
    options.headers = {
        ...(options.headers || {}),
        token: localStorage.token
    }
    return fetch(baseUrl + url, options)
        .then(response => {
            if (response.status == 200) {
                return response.json()
            } else if (response.status == 401) {
                localStorage.clear();
                // location.pathname = "/login"
            }
        })
        .then(res => {
            if (res.code == 1) {
                //数据正常
                return res.data;
            } else {
                message.error(res.msg || "接口错误")
                return Promise.reject(res)
            }
        })
}

export default request;